<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        style="width: 130px;"
        class="filter-item"
        placeholder="请输入银行名称"
        v-model="queryForm.bankName"
      >
      </el-input>
      <el-input
        style="width: 130px;"
        class="filter-item"
        placeholder="请输入银行编码"
        v-model="queryForm.bankCode"
      >
      </el-input>
      <el-select
        clearable
        style="width: 135px"
        class="filter-item"
        v-model="queryForm.status"
        placeholder="请选择状态"
      >
        <el-option
          v-for="(val, key) in typeOptions"
          :key="key"
          :label="val"
          :value="key"
        >
        </el-option>
      </el-select>
      <el-button
        class="filter-item"
        @click="doQuery"
        type="primary"
        icon="el-icon-search"
        >查询</el-button
      >
      <el-button
        v-permission="'bank:add'"
        class="filter-item"
        style="margin-left: 10px;"
        @click="openAddDialog"
        type="success"
        icon="el-icon-plus"
        >新增</el-button
      >
    </div>
    <el-table v-loading="loading" :data="records" style="width: 100%">
      <el-table-column
        prop="bankName"
        label="银行名称"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="bankCode"
        label="银行编码"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="bankIcon"
        label="银行图标"
        width="250"
        align="center"
      >
        <template slot-scope="scope">
          <img
            :src="$oss(scope.row.bankIcon)"
            alt
            style="width: 150px;height: 50px"
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="maxTradeAmount"
        label="最大交易额"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="status" label="状态" width="150" align="center">
        <template slot-scope="scope">
          {{ Dicts.CONSUMER_STATUS[scope.row.status] }}
        </template>
      </el-table-column>
      <el-table-column
        prop="modifyTime"
        label="修改时间"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        class-name="small-padding"
        fixed="right"
        width="250"
      >
        <template slot-scope="scope">
          <el-button
            v-permission="'bank:update'"
            type="primary"
            size="mini"
            @click="editWithdrawBank(scope.row)"
            title="编辑"
            icon="el-icon-edit"
            circle
          ></el-button>
          <el-button
            v-permission="'bank:delete'"
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.rowId)"
            title="删除"
            icon="el-icon-delete"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryForm.current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryForm.total"
      >
      </el-pagination>
    </div>

    <el-dialog
      :title="'新增提现银行'"
      :visible.sync="dialogAddFormVisible"
      width="50%"
    >
      <el-form
        :model="addForm"
        label-width="160px"
        :rules="addFormRules"
        ref="addForm"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="银行名称" prop="bankName">
              <el-input
                v-model="addForm.bankName"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="银行编码" prop="bankCode">
              <el-input
                v-model="addForm.bankCode"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="银行图标" prop="bankIcon">
              <el-upload
                v-model="addForm.bankIcon"
                class="upload-demo"
                action=""
                :data="{ dir: '/banks/' + this.imgDate }"
                :file-list="files"
                :http-request="upload"
                :on-success="uploadBank"
                list-type="picture"
                accept="image/*"
                :limit="1"
              >
                <el-button size="small" type="primary">选择图标</el-button>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="最大交易额" prop="maxTradeAmount">
              <el-input
                v-model="addForm.maxTradeAmount"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogAddFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="execAddForm">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :title="'编辑提现银行'"
      :visible.sync="dialogEditFormVisible"
      width="50%"
    >
      <el-form
        :model="editForm"
        label-width="160px"
        :rules="editFormRules"
        ref="editForm"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="银行名称" prop="bankName">
              <el-input
                v-model="editForm.bankName"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="银行编码" prop="bankCode">
              <el-input
                v-model="editForm.bankCode"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- <el-row>
          <el-col :span="12">
            <el-form-item label="银行图标" prop="bankIcon" >
              <el-input v-model="editForm.bankIcon" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row> -->

        <el-row>
          <el-col :span="12">
            <el-form-item label="最大交易额" prop="maxTradeAmount">
              <el-input
                v-model="editForm.maxTradeAmount"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-select
                v-model="editForm.status"
                placeholder=""
                style="width: 100%;"
              >
                <el-option
                  v-for="(val, key) in Dicts.CONSUMER_STATUS"
                  :key="key"
                  :label="val"
                  :value="key"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="execEditForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryWithdrawBank,
  deleteWithdrawBank,
  addWithdrawBank,
  editWithdrawBank
} from '@/api/account'

export default {
  name: 'withdrawBank',
  data() {
    return {
      imgDate: '',
      dialogAddFormVisible: false,
      dialogEditFormVisible: false,
      loading: false,
      files: [],
      queryForm: {
        current: 1,
        pages: 0,
        total: 0,
        size: 10
      },
      addFormRules: {
        bankName: [
          { required: true, message: '请输入银行名称', trigger: 'blur' }
        ],
        bankCode: [
          { required: true, message: '请选择银行编码', trigger: 'blur' }
        ],
        bankIcon: [
          { required: true, message: '请选择银行图标', trigger: 'blur' }
        ],
        maxTradeAmount: [
          { required: true, message: '请输入最大限额', trigger: 'blur' }
        ]
      },
      addForm: {},
      editFormRules: {
        bankName: [
          { required: true, message: '请输入银行名称', trigger: 'blur' }
        ],
        bankCode: [
          { required: true, message: '请选择银行编码', trigger: 'blur' }
        ],
        maxTradeAmount: [
          { required: true, message: '请输入最大限额', trigger: 'blur' }
        ],
        status: [{ required: true, message: '请选择状态', trigger: 'blur' }]
      },
      editForm: {},
      typeOptions: Dicts.CONSUMER_STATUS,
      records: []
    }
  },
  created() {
    // 初始化一次oss client,如果初始化过一次返回上一次初始化对象
    this.initOssClient().then(_ => {
      this.doQuery()
    })
  },
  methods: {
    upload(options) {
      this.uploadForElement(options)
      this.addForm.bankIcon = options.data.dir + '/' + options.file.name
    },
    uploadBank(response, file, fileList) {
      this.addForm.bankIcon = response.name
    },
    handleCurrentChange(current) {
      this.queryForm.current = current
      this.doQuery()
    },
    handleSizeChange(size) {
      this.queryForm.size = size
      this.doQuery()
    },
    doQuery() {
      this.loading = true
      queryWithdrawBank(this.queryForm).then(({ data: { data } }) => {
        this.queryForm.current = data.current
        this.queryForm.pages = data.pages
        this.queryForm.total = data.total
        this.queryForm.size = data.size
        this.records = data.records
        this.loading = false
      })
      this.imgDate =
        new Date().getFullYear() +
        '' +
        (new Date().getMonth() + 1) +
        '' +
        new Date().getDate()
    },
    handleDelete(rowId) {
      this.$confirm('此操作将删除该提现银行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteWithdrawBank(rowId).then(({ data: { data, msg } }) => {
          this.doQuery()
          this.$message({
            showClose: true,
            message: msg,
            type: 'success'
          })
        })
      })
    },
    openAddDialog() {
      this.addForm = {}
      this.files = []
      this.dialogAddFormVisible = true
      this.$refs.addForm && this.$refs.addForm.resetFields()
    },
    execAddForm() {
      this.$refs.addForm.validate(valid => {
        if (valid) {
          this.addForm.status = '1'
          addWithdrawBank(this.addForm).then(
            ({ data: { code, msg, data } }) => {
              if (code === 0) {
                this.doQuery()
                this.dialogAddFormVisible = false
              }
              this.$message({
                showClose: true,
                message: msg,
                type: code === 0 ? 'success' : 'error'
              })
            }
          )
        } else {
          return false
        }
      })
    },
    openEditDialog() {
      this.editForm = {}
      this.dialogEditFormVisible = true
      this.$refs.editForm && this.$refs.editForm.resetFields()
    },
    editWithdrawBank(row) {
      this.openEditDialog()
      this.editForm = {
        rowId: row.rowId,
        bankName: row.bankName,
        bankCode: row.bankCode,
        bankIcon: row.bankIcon,
        maxTradeAmount: row.maxTradeAmount,
        status: row.status
      }
    },
    execEditForm() {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          editWithdrawBank(this.editForm).then(
            ({ data: { code, msg, data } }) => {
              if (code === 0) {
                this.doQuery()
                this.dialogEditFormVisible = false
              }
              this.$message({
                showClose: true,
                message: msg,
                type: code === 0 ? 'success' : 'error'
              })
            }
          )
        } else {
          return false
        }
      })
    }
  },
  components: {}
}
</script>

<style scoped></style>
